package modele;

import java.awt.Color;
import java.io.Serializable;
import java.rmi.RemoteException;
import java.util.ArrayList;

import codebase.IPartie;

public class Puissance implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String nom;
	private String abreviation;
	private Color couleur;
	private String adrImage;
	private ArrayList<Region> listeregionDepart;
	char[] typeUniteDepart;
	private ArrayList<Region> listeRegionsPossedes;
	private ArrayList<Unite> listeUnite;

	public ArrayList<Unite> getListeUnite() {
		return listeUnite;
	}

	public Puissance(String nom, String abreviation, Color couleur, String adrImage,
			char[] typeUnite, Region... tabRegionDep) {
		this.nom = nom;
		this.abreviation = abreviation;
		this.couleur = couleur;
		this.adrImage = adrImage;
		listeregionDepart = new ArrayList<Region>();
		for (int i = 0; i < tabRegionDep.length; i++) {
			listeregionDepart.add(tabRegionDep[i]);
		}
		listeRegionsPossedes = new ArrayList<Region>();
		listeUnite = new ArrayList<Unite>();
		typeUniteDepart = typeUnite;
		try {
			initDepart();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void initDepart() throws RemoteException {

		for (Region region : listeregionDepart) {
			listeRegionsPossedes.add(region);
		}
		listeRegionsPossedes = listeregionDepart;
		int i = 0;
		for (Region region : listeregionDepart) {
			Unite unite;
			if (typeUniteDepart[i] == 'a')
				unite = nouvelleUnite(region, true);
			else
				unite = nouvelleUnite(region, false);
			listeUnite.add(unite);
			IPartie.carte.get(region.getAbreviation().toLowerCase()).setOccupant(unite);
			i++;
		}

	}

	public Unite nouvelleUnite(Region region, boolean terrestre) throws RemoteException {
		Unite unite;
		if (terrestre)
			unite = new Armee(this);
		else
			unite = new Flotte(this);
		return unite;
	}

	public ArrayList<Region> getListeregionDepart() {
		return listeregionDepart;
	}

	public ArrayList<Region> getListeRegionsPossedes() {
		return listeRegionsPossedes;
	}

	public void addRegion(Region r) {
		listeRegionsPossedes.add(r);
	}

	public String getAdrImage() {
		return adrImage;
	}

	public String getNom() {
		return this.nom;
	}

	public String getAbreviation() {
		return this.abreviation;
	}

	public Color getCouleur() {
		return this.couleur;
	}

	public String toString() {
		return abreviation;
	}

	public int getScore() {
		int score = 0;
		for (Region region : listeRegionsPossedes) {
			if (region.isRavitaillement())
				score++;
		}
		return score;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Puissance other = (Puissance) obj;
		if (nom == null) {
			if (other.nom != null)
				return false;
		} else if (!nom.equals(other.nom))
			return false;
		return true;
	}

	public void nettoieCarte() {
		for (Region region : listeRegionsPossedes) {
			region.setOccupant(null);
		}

	}
}
